package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.base.TbSocialSecurityPay;
import com.ys.entity.base.TbSocialSecurityPayItem;
import com.ys.service.base.TbSocialSecurityPayService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/base/tbSocialSecurityPay")
public class WebTbSocialSecurityPayController {


	@Autowired
	private CommonApiService<TbSocialSecurityPay> commonApiService;

	@Autowired
	private TbSocialSecurityPayService tbSocialSecurityPayService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TB_SocialSecurityPay);
			TbSocialSecurityPay tbSocialSecurityPay = new TbSocialSecurityPay()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_SocialSecurityPay, new ArrayList<Map<String, Object>>());

			String aa = commonApiService.toAdd(model, tbSocialSecurityPay, MenuId.TB_SocialSecurityPay, null, subGridConfig);

			return "base/tbReservedPay/tbReservedPayAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params) throws Exception {
		if (params.get("bid") == null){
			return W.error(model, "单号不能为空");
		}

		String bid = (params.get("bid").toString());

		Map<String, Object> extraParam =tbSocialSecurityPayService.getExtraParam(bid);
		if (extraParam == null){
			return W.error(model, "社保付款单不存在!");
		}
		List<Map<String, Object>> sonlist = tbSocialSecurityPayService.getItemList(bid);

		// 子列表配置信息
		SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_SocialSecurityPay, sonlist);

		String aa = commonApiService.toEdit(model, Long.valueOf(extraParam.get("id").toString()), new TbSocialSecurityPay(), MenuId.TB_SocialSecurityPay, extraParam, subGridConfig);

		return "base/tbReservedPay/tbReservedPayEdit";
	}

}
